// __PUBLIC__/js/page/admin/content/apply
require(['../../common'], function() {
  require(['content.apply'])
})

define('content.apply', [
  'jquery',
  'page/grid'
], function($, Grid) {
  'use strict';

  var $table = $('.table')
  var mygrid = new Grid($table)
  mygrid.init()

  $(document)
    .on('click', '.A_apply', function(e) {
      e.preventDefault()
      doapply(1, getASelected(this))
    })
    .on('click', '.A_reject', function(e) {
      e.preventDefault()
      doapply(2, getASelected(this))
    })
    .on('click', '#J_apply', function(e) {
      e.preventDefault()
      doapply(1, getSelected())
    })
    .on('click', '#J_reject', function(e) {
      e.preventDefault()
      doapply(2, getSelected())
    })
    .on('click', '#J_search', function(e) {
      e.preventDefault()
      sureSearchApply()
    })

  function getSelected() {
    var selected = mygrid.getSelected()
    if (!selected.count) {
      alert(lang.pleaseSelectAData)
      return
    }
    return selected
  }

  function getASelected(t) {
    var current = mygrid.getCurrent(t)
    var selected = {
      id: [current.id],
      index: [current.index]
    }
    return selected
  }

  //status 1-通过 2-拒绝

  function doapply(status, selected) {
    $.post(APP + '?app=admin&m=Content&a=doapply', {
      ids: selected.id.join(),
      status: status
    }).done(function(res) {
      if (res == '1') {
        alert(lang.delSuccess)
        location.href = APP + '?app=admin&m=Content&a=apply'
      } else {
        alert(lang.delFails)
      }
    });
  }

  function sureSearchApply() {
    var id = $('#id').val()
    var email = $('#email').val()
    var status = $('#status').val()

    var str_get = '&id=' + id + '&email=' + email + '&status=' + status
    location.href = APP + '?app=admin&m=Content&a=apply' + str_get
  }
})
